<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>

	<body>
		<h1>数组</h1>
		<p>
			JS中数组没有越界的概念,如果没有值,会返回undefined值
			所有的索引其实都是属性名
			稀疏数组:从0开始不连续索引的数组
		</p>
		<script type="text/javascript">
			var xxp = {a:"xxx"};
			console.log(xxp['a']);
		
			var a = new Array(10);
			//不受原始大小的约束
			a[11] = 1;
			a[12] = 2;
			a[-123] = 4;//其实是创建了一个名为"-123"的属性
			a[-122] = 5;//非正整 数不算在length中
			window.onload = function(){
				console.log(a.toString());//,....,1,2
				console.log(a[-122]);//5
				console.log(a.length);//13
				var arr = [1,2,3,4,5];
				arr.length = 3;
				console.log("three: "+arr.toString()+" len="+arr.length);
				arr.length = 0;
				console.log("zero: "+arr.toString()+" len="+arr.length);
				arr.length = 5;
				console.log("five: "+arr.toString()+" len="+arr.length);
				//通过push往数组尾添加元素
				arr.push("a");
				//push返回的是新数组的长度
				arr.push("b","c");
				console.log(arr.toString());//,,,,,a,b,c
				delete arr[5]; //置为undefined,不影响数组长度
				console.log(arr.toString()+" len="+arr.length);//a,b,c
				//弹出数组最后一个元素,数组长度变短
				//pop返回的是删除的元素
				arr.pop();
				console.log(arr.toString()+" len="+arr.length);//,,,,,,b
				//弹出头部元素,数组长度变短
				arr.shift();
				console.log(arr.toString()+" len="+arr.length);//,,,,,b
				//在数组头部添加元素
				arr.unshift("d");
				console.log(arr.toString()+" len="+arr.length);//d,,,,,,b
				//获取对象的所有属性
				arr[-79] = "new value";
				var keys = Object.keys(arr);
				for(var i=0;i<keys.length;i++){
					console.log(keys[i]+" : "+arr[keys[i]]);
				}
				arr.forEach(function(x){
					console.log("v : "+x);//只能循环非负整数的索引
				});
				//将数组连接成字符串,默认使用逗号连接 
				var s1 = arr.join();
				console.log(s1);//d,,,,,b
				var s2 = arr.join("-");
				console.log(s2);//d-----b
				var s3 = arr.reverse();
				console.log(s3.join());//b,,,,,,d
				var arr1 = ["ap","cd","bg","xf"];
				console.log(arr1.sort().toString());
				//可以用匿名函数来指定排序规则
				console.log(arr1.sort(function(a,b){
					if(a>b) return -1;
					if(a<b) return 1;
					return 0;
				}).toString());
				var s4 = arr1.concat("ap",["aa",["bb","cc"]]);
				console.log(s4);
				//slice不会改变数组大小
				console.log(s4.slice(-5,-4));//截取倒数第五个至倒数第四个之前
				//splice会改变数组大小
				var s5 = arr1.splice(2,3);//从索引2处删除3个元素
				console.log("删除后 ："+s4);
				console.log("返回值："+s5);
				arr1.splice(2,0,["a","b"]);//从索引2处删除0个,并添加一个新数组
				console.log("操作后："+s4);
				console.log("-----------------");
				//forEach
				arr1.forEach(function(v,i,a){
					console.log("v: "+ v);//当前值 
					console.log("i: "+ i);//当前索引
					console.log("a: "+ a);//正在被循环的数组
				});
				//map返回的是新数组,没有改变原数组,新数组的长度和类型和原来的一样 
				var a1 = [1,2,3];
				var b1 = a1.map(function(x){return x*x;});
				console.log("b1: " + b1);//[1,4,9]
				//filter返回原数组的子集 ,并且总是密稠的,而不是稀疏的
				b1 = a1.filter(function(x){return x>=2;});
				console.log("b1: " + b1);//[2,3]
				//every()判断数组元素是否都符合条件,some()判断是否存在符合添加的元素
				var f1 = a1.every(function(x){return x>0;});//当遇到false就返回 
				console.log("f1: all > 0 "+f1);//true 
				var f2 = a1.some(function(x){return x==0;});//当遇到true就返回
				console.log("f2: =0 "+f2);//false
				//reduce(),reduceRight()
				var sum = a1.reduce(function(x,y){return x+y;},100);//2个参数,执行函数和初始化值[可选]
				console.log("sum:" + sum);//106
				console.log("indexOf(2): "+a1.indexOf(2,0));//1 从0开始搜索值为2的索引
				var f3 = Array.isArray(a1);//判断对象是否是数组
				console.log(f3);
				//类数组对象 
				var o1 = {};
				o1[0]=1;
				o1.length = 1;//截断 
				for(var i=0;i<o1.length;i++){
					console.log(o1[i]);
				}
				console.log("o1 is Array? "+Array.isArray(o1)+" type:"+Object.prototype.toString.call(o1));
				var o2 = {"0":"a", "1":"b", "2":"c", length:3};
// 				var v1 = Array.prototype.slice.call(o2,0);
				var v1 = Array.slice(o2,0);//FF专属方法
				console.log(v1);
				var v2 = Array.prototype.map.call(o2,function(x){return x.toUpperCase();});
				console.log(v2);
				
			};
			
		</script>
	</body>

</html>